package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.respository.IStrategyCommentRepository;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Created by Administrator on 2019/7/11.
 */
@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService{
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IStrategyCommentRepository dao;
    @Override
    public void save(StrategyComment strategyComment) {
        //游记标题
        //创建时间
        if (strategyComment.getId()==null){
            strategyComment.setCreateTime(new Date());
        }
        //点赞数
        //评论点赞的人
        dao.save(strategyComment);
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        Query query = new Query();
        //设置参数
        Criteria criteria = null;
        if (qo.getDetailId()!=-1){
            criteria=Criteria.where("detailId").is(qo.getDetailId());
        }else {
            criteria=Criteria.where("detailId").ne(qo.getDetailId());
        }


        if (qo.getKeyword() != null){
            criteria.and("content").regex("^.*"+qo.getKeyword()+".*$");
        }
        query.addCriteria(criteria);//添加查询条件
        //查询总条数
        long total = mongoTemplate.count(query, StrategyComment.class);
        if (total==0){
            return Page.empty();
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());
        query.with(pageable);
        //排序
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        query.with(sort);
        List<StrategyComment> list = mongoTemplate.find(query, StrategyComment.class);


        PageImpl<StrategyComment> pageImpl = new PageImpl<StrategyComment>(list,pageable,total);
        return pageImpl;
    }

    @Override
    public boolean thumbUp(String toid, Long fromid) {
        Optional<StrategyComment> optional = dao.findById(toid);
        StrategyComment strategyComment = optional.get();
        List<Long> thumbuplist = strategyComment.getThumbuplist();
        boolean flag = false;
        //点赞
        if (!thumbuplist.contains(fromid)){
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()+1);
            thumbuplist.add(fromid);
            flag=true;
        }else {
            //取消点赞
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()-1);
            thumbuplist.remove(fromid);
        }
        this.save(strategyComment);

        return flag;
    }
}
